﻿using System;
using System.Collections.Generic;
using System.Text;
using KeaoBase;
using System.Data;
using GoldPrinter;
using System.Windows.Forms;
using ThoughtWorks.QRCode.Codec;
using System.Drawing;
using System.IO;

namespace KeaoMDS.AppCode
{
    public class KeaoPrinter
    {

        #region 打印

        private static string m_DaYinJieSuanZhongLiang = KeaoUtils.ConfigGetNodeAttr("系统配置", "打印结算重量");

        public static void Print(object ChengZhongID, bool IsBuDa)
        {
            ExcelAccess printExcel = new ExcelAccess();
            try
            {

                string createCondition = KeaoUtils._DB.CreateAndCondition("id", DataValueCommonType.字符串, string.Format("{0}", ChengZhongID), "", SearchConditionType.等于);
                DataTable Dt = KeaoUtils._DB.GetDataTableByCondition("tbl_chengzhong", createCondition);
                if (Dt == null || Dt.Rows.Count == 0)
                {
                    return;
                }
                DataRow printRow = Dt.Rows[0];

                string DanHao = string.Format("{0}", printRow["chengzhongbianhao"]);
                if (IsBuDa)
                {
                    string riZhiStr = string.Format("补打磅单，单号【{0}】", DanHao);
                    if (DanHao.Contains("_"))
                    {
                        string[] tmpStrs = DanHao.Split('_');
                        DanHao = string.Format("{0}_{1}", int.Parse(tmpStrs[0]) + 1, tmpStrs[1]);
                    }
                    else
                    {
                        DanHao = string.Format("1_{0}", DanHao);
                    }
                    TableRow editRow = new TableRow() { TableName = "tbl_chengzhong", ID = string.Format("{0}", printRow["id"]), Ctl = DBCtl.修改 };
                    editRow.DataCols.Add(new TableCol() { ColName = "chengzhongbianhao", ValueMember = DanHao, ValueType = DataValueCommonType.字符串 });
                    editRow.DataCols.Add(new TableCol() { ColName = "shangchuanzhuangtai", ValueMember = "0", ValueType = DataValueCommonType.字符串 });
                    KeaoUtils._DB.SaveTableRows(editRow);
                    MainForm.RiZhi(string.Format("{0}，补打后【{1}】", riZhiStr, DanHao));
                }

                printExcel.Open(Application.StartupPath + "\\Template\\磅单.xls");
                QRCodeEncoder m_QRCode = new QRCodeEncoder();
                m_QRCode.QRCodeScale = 7;
                m_QRCode.QRCodeVersion = 5;
                Bitmap img = m_QRCode.Encode(string.Format("http://keaomds.cn:9500/d.aspx?b={0}&u={1}&l={2}&f={3}", DanHao, KeaoUtils.ConfigGetNodeAttr("客户", "客户编号"), KeaoUtils.ConfigGetNodeAttr("客户", "站点").Replace("#", "%23"), KeaoUtils.ConfigGetNodeAttr("客户", "磅房").Replace("#", "%23")), Encoding.UTF8);
                if (File.Exists("QR.jpg"))
                {
                    File.Delete("QR.jpg");
                }
                img.Save("QR.jpg");
                printExcel.SetCellText(1, "A", string.Format("{0}{1}", KeaoUtils.ConfigGetNodeAttr("客户", "项目部"), KeaoUtils.ConfigGetNodeAttr("客户", "磅房")));
                printExcel.InsertPicture(5, "K", Application.StartupPath + @"\QR.jpg", 90f, 90f, 470f, 90f);
                printExcel.SetCellText(6, "A", printRow["chengzhongleixing"].Equals("入库物料") ? "供料单位" : "收料单位");
                printExcel.SetCellText(3, "A", string.Format(printExcel.GetCellText(3, "A"), string.Format("{0}_{1}", KeaoUtils.ConfigGetNodeAttr("客户", "磅房"), DanHao), printRow["chepaihao"]));
                printExcel.SetCellText(5, "A", string.Format("{0}", printRow["wuliaomingcheng"]));
                printExcel.SetCellText(5, "B", string.Format("{0}", printRow["xinghao"]));
                printExcel.SetCellText(5, "C", string.Format("{0}", printRow["maozhong"]));
                printExcel.SetCellText(5, "E", string.Format("{0}", printRow["pizhong"]));
                printExcel.SetCellText(5, "G", string.Format("{0}", printRow["kouzhong"]));
                printExcel.SetCellText(5, "I", string.Format("{0}", printRow["jingzhong"]));
                printExcel.SetCellText(8, "A", string.Format(printExcel.GetCellText(8, "A"), printRow["maozhongshijian"], printRow["pizhongshijian"]));
                if (printRow["quxiangbiaoduan"].Equals(""))
                {
                    printExcel.SetCellText(6, "B", string.Format("{0}", printRow["danweimingcheng"]));
                }
                else
                {
                    createCondition = KeaoUtils._DB.CreateAndCondition("id", DataValueCommonType.字符串, string.Format("{0}", printRow["quxiangbiaoduan"]), "", SearchConditionType.等于);
                    DataTable tmpDt = KeaoUtils._DB.GetDataTableByCondition("tbl_biaoduan", createCondition);
                    printExcel.SetCellText(6, "B", string.Format("{0}({1}_{2}_{3})", printRow["danweimingcheng"], tmpDt.Rows[0]["zuoyoufu"], tmpDt.Rows[0]["gongcheng"], tmpDt.Rows[0]["biaoduanmingcheng"]));
                }
                printExcel.SetCellText(7, "B", string.Format("{0}", printRow["beizhu"]));
                printExcel.SetCellText(3, "I", string.Format(printExcel.GetCellText(3, "I"), DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
                if (m_DaYinJieSuanZhongLiang == "启用")
                {
                    decimal a = Math.Truncate(Convert.ToDecimal(printRow["jingzhong"]) * 10m);
                    decimal b = a / 10m;
                    string c = b.ToString("0.0");
                    printExcel.SetCellText(6, "I", c);
                }
                printExcel.Print();
            }
            catch (Exception err)
            {
                KeaoUtils.LogTxt(string.Format("打印异常。{0}", err.Message));
            }
            finally
            {
                printExcel.Close();
            }
        }

        public static void PrintTuiKu(object TuiKuID)
        {
            ExcelAccess printExcel = new ExcelAccess();

            try
            {

                string createCondition = KeaoUtils._DB.CreateAndCondition("id", DataValueCommonType.字符串, string.Format("{0}", TuiKuID), "", SearchConditionType.等于);
                DataTable Dt = KeaoUtils._DB.GetDataTableByCondition("tbl_tuiku", createCondition);
                if (Dt == null || Dt.Rows.Count == 0)
                {
                    return;
                }
                DataRow printRow = Dt.Rows[0];
                printExcel.Open(Application.StartupPath + "\\Template\\退库单.xls");
                string DanHao = string.Format("{0}", printRow["tuikubianhao"]);
                printExcel.SetCellText(1, "A", string.Format("{0}{1}", KeaoUtils.ConfigGetNodeAttr("客户", "项目部"), KeaoUtils.ConfigGetNodeAttr("客户", "磅房")));
                printExcel.SetCellText(6, "A", printRow["tuikuleixing"].Equals("入库物料") ? "供料单位" : "收料单位");
                printExcel.SetCellText(3, "A", string.Format(printExcel.GetCellText(3, "A"), string.Format("{0}_{1}", KeaoUtils.ConfigGetNodeAttr("客户", "磅房"), DanHao), printRow["tuikuchepaihao"]));
                printExcel.SetCellText(5, "A", string.Format("{0}", printRow["tuikuwuliao"]));
                printExcel.SetCellText(5, "B", string.Format("{0}", printRow["tuikuxinghao"]));
                printExcel.SetCellText(5, "C", string.Format("{0}", printRow["tuikumaozhong"]));
                printExcel.SetCellText(5, "E", string.Format("{0}", printRow["tuikupizhong"]));
                printExcel.SetCellText(5, "G", string.Format("{0}", printRow["tuikujingzhong"]));
                printExcel.SetCellText(8, "B", string.Format("{0}", printRow["tuikushijian"]));
                printExcel.SetCellText(6, "B", string.Format("{0}", printRow["tuikubiaoduan"].Equals("") ? printRow["tuikudanwei"] : string.Format("{0}({1})", printRow["tuikudanwei"], printRow["tuikubiaoduan"])));
                printExcel.SetCellText(7, "B", string.Format("{0}", printRow["tuikubeizhu"]));
                printExcel.SetCellText(3, "E", string.Format(printExcel.GetCellText(3, "E"), DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));

                printExcel.Print();
            }
            catch (Exception err)
            {
                KeaoUtils.LogTxt(string.Format("打印异常。{0}", err.Message));
            }
            finally
            {
                printExcel.Close();
            }
        }

        #endregion
    }
}
